<template>
  <el-row class="min-h-screen bg-amber-200">
    <el-col :lg="16" :md="12" class="login-left">
      <div class="left-top">欢迎光临</div>
      <div class="left-top left-two">vue3+vite+pinia</div>
    </el-col>
    <el-col :lg="8" :md="12" class="login-right">
      <h2 class="font-bold text-3xl text-red-50">欢迎回来</h2>
      <div class="right-one">
        <span class="h-[1px] w-16 bg-gray-200"></span>
        <span>账号密码登录</span>
        <span class="h-[1px] w-16 bg-gray-200"> </span>
      </div>

      <el-form ref="fromRef" :rules="rules" :model="form" class="w-[250px]">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            type="username"
            placeholder="请输入密码"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><UserFilled /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="onSubmit"
            class="w-[250px]"
            round
            :loading="loading"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script name='' setup>
import { reactive, ref } from "vue";
import { login, getinfo } from "@/http/api/manager.js";
import { useRouter } from "vue-router";
import { toast } from '@/composables/util.js'
import { setToken }from '../../composables/auth'
import { useStore } from 'vuex'

const store = useStore()

const route = useRouter();

// do not use same name with ref
const form = reactive({
  username: "",
  password: "",
});

const fromRef = ref(null);
const loading = ref(false)

//定义规则
const rules = {
  username: [
    { required: true, message: "名字不能为空", trigger: "blur" },
    { min: 3, max: 10, message: "长度为3-10位", trigger: "blur" },
  ],
  password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
};

//点击登录
const onSubmit = async () => {
  fromRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      login(form)
        .then((res) => {
          toast('登录成功')
          window.sessionStorage.setItem("token", res.token);
          setToken(res.token);
          //获取用户相关信息
          getinfo().then((res) => {
            store.commit('SET_USERINFO',res)
            console.log(res);
          });
          //成功后跳转路由
            route.push("/home");
          })
        .finally(() => {
          loading.value = false;
        });
    }
    
    return false;
  });
};
</script>

<style scoped lang="scss">
.login-left {
  @apply flex items-center justify-center flex-col;
  background-image: url("@/assets/bg.gif");
  > .left-top {
    color: #fff;
    @apply font-bold text-5xl;
  }
  > .left-two {
    @apply font-bold text-2xl tracking-widest;
  }
}

.login-right {
  @apply bg-indigo-50 flex items-center justify-center flex-col;
  background-image: linear-gradient(to right, #434343 0%, black 100%);
  color: #fff;
  .right-one {
    @apply flex items-center justify-center my-5 text-gray-300 space-x-2;
  }
}
</style>